package com.ss.android.common.imagezoom;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewConfiguration;
import com.bytedance.common.utility.Logger;

public class ImageViewTouch extends ImageViewTouchBase {
    public final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private final ImageViewTouch a;

        public GestureListener(ImageViewTouch arg1) {
            this.a = arg1;
        }

        public final boolean onDoubleTap(MotionEvent arg6) {
            Logger.i("image", "onDoubleTap. double tap enabled? " + this.a.d);
            if(this.a.d) {
                float v0 = Math.min(this.a.getMaxZoom(), Math.max(this.a.a(this.a.getScale(), this.a.getMaxZoom()), this.a.getMinZoom()));
                this.a.b = v0;
                this.a.a(v0, arg6.getX(), arg6.getY(), 200f);
                this.a.invalidate();
            }

            if(this.a.f != null) {
                this.a.f.a.h.a("zoom_in");
            }

            return super.onDoubleTap(arg6);
        }

        public final boolean onFling(MotionEvent arg2, MotionEvent arg3, float arg4, float arg5) {
            return this.a.b(arg2, arg3, arg4, arg5);
        }

        public final void onLongPress(MotionEvent arg3) {
            if((this.a.isLongClickable()) && !this.a.a.isInProgress()) {
                this.a.setPressed(true);
                this.a.performLongClick();
            }
        }

        public final boolean onScroll(MotionEvent arg2, MotionEvent arg3, float arg4, float arg5) {
            return this.a.a(arg2, arg3, arg4, arg5);
        }

        public final boolean onSingleTapConfirmed(MotionEvent arg3) {
            boolean v0;
            if(this.a.h == null || !this.a.g) {
                v0 = super.onSingleTapConfirmed(arg3);
            }
            else {
                this.a.h.onClick(this.a);
                v0 = true;
            }

            return v0;
        }
    }

    public static final class b {
        private final com.ss.android.image.n.b a;

        public b(com.ss.android.image.n.b arg1) {
            this.a = arg1;
        }
    }

    public final class c extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        private final ImageViewTouch a;

        public c(ImageViewTouch arg1) {
            this.a = arg1;
        }

        public final boolean onScale(ScaleGestureDetector arg7) {
            boolean v0 = true;
            Log.d("image", "onScale");
            float v1 = this.a.b * arg7.getScaleFactor();
            if(this.a.e) {
                v1 = Math.min(this.a.getMaxZoom(), Math.max(v1, this.a.getMinZoom() - 0.1f));
                this.a.a(v1, arg7.getFocusX(), arg7.getFocusY());
                this.a.b = Math.min(this.a.getMaxZoom(), Math.max(v1, this.a.getMinZoom() - 1f));
                this.a.c = 1;
                this.a.invalidate();
            }
            else {
                v0 = false;
            }

            return v0;
        }
    }

    protected ScaleGestureDetector a;
    protected float b;
    protected int c;
    protected boolean d;
    protected boolean e;
    b f;
    protected boolean g;
    protected View.OnClickListener h;
    private GestureDetector n;
    private float o;
    private GestureDetector.OnGestureListener p;
    private ScaleGestureDetector.OnScaleGestureListener q;
    private boolean r;

    public ImageViewTouch(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.d = true;
        this.e = true;
        this.r = true;
        this.g = true;
    }

    protected final float a(float arg3, float arg4) {
        if(this.c != 1) {
            this.c = 1;
            arg4 = 1f;
        }
        else if(this.o * 2f + arg3 <= arg4) {
            arg4 = arg3 + this.o;
        }
        else {
            this.c = -1;
        }

        return arg4;
    }

    protected final void a() {
        super.a();
        ViewConfiguration.get(this.getContext()).getScaledTouchSlop();
        this.p = this.getGestureListener();
        this.q = this.getScaleListener();
        this.a = new ScaleGestureDetector(this.getContext().getApplicationContext(), this.q);
        this.n = new GestureDetector(this.getContext().getApplicationContext(), this.p, null, true);
        this.b = 1f;
        this.c = 1;
    }

    protected final void a(float arg2) {
        super.a(arg2);
        if(!this.a.isInProgress()) {
            this.b = arg2;
        }
    }

    protected final void a(Drawable arg3) {
        super.a(arg3);
        float[] v0 = new float[9];
        this.i.getValues(v0);
        this.b = v0[0];
    }

    protected final void a(Drawable arg3, boolean arg4, Matrix arg5, float arg6) {
        super.a(arg3, arg4, arg5, arg6);
        this.o = this.getMaxZoom() / 3f;
    }

    public final boolean a(int arg7) {
        boolean v0 = false;
        if(this.getDrawable() != null) {
            RectF v2 = this.getBitmapRect();
            if(v2 != null) {
                this.a(v2, this.l);
                Rect v3 = new Rect();
                this.getGlobalVisibleRect(v3);
                if(v2.right >= (((float)v3.right)) && arg7 < 0) {
                    if(Math.abs(v2.right - (((float)v3.right))) > 1f) {
                        v0 = true;
                    }
                    else {
                    }

                    return v0;
                }

                if((((double)Math.abs(v2.left - this.l.left))) <= 1) {
                    return v0;
                }

                v0 = true;
            }
        }

        return v0;
    }

    public final boolean a(MotionEvent arg7, MotionEvent arg8, float arg9, float arg10) {
        boolean v0 = false;
        if((this.r) && arg7 != null && arg8 != null && arg7.getPointerCount() <= 1 && arg8.getPointerCount() <= 1 && !this.a.isInProgress()) {
            float v2 = this.getScale();
            if(Logger.debug()) {
                Logger.d("image", "onScroll: " + arg9 + ", " + arg10 + " " + v2);
            }

            if(v2 == 1f && !this.k) {
                return v0;
            }

            this.c(-arg9, -arg10);
            this.invalidate();
            v0 = true;
        }

        return v0;
    }

    protected final void b(float arg3) {
        super.b(arg3);
        if(!this.a.isInProgress()) {
            this.b = arg3;
        }

        if(arg3 < this.getMinZoom()) {
            this.b(this.getMinZoom(), 50f);
        }
    }

    public final boolean b() {
        boolean v0 = this.b <= 1f ? true : false;
        return v0;
    }

    public final boolean b(MotionEvent arg8, MotionEvent arg9, float arg10, float arg11) {
        float v6 = 800f;
        float v5 = 2f;
        boolean v0 = false;
        if(this.r) {
            if(Logger.debug()) {
                Logger.d("image", "onFling: " + arg10 + ", " + arg11);
            }

            if(arg8.getPointerCount() > 1) {
                return v0;
            }

            if(arg9.getPointerCount() > 1) {
                return v0;
            }

            if(this.a.isInProgress()) {
                return v0;
            }

            float v2 = arg9.getX() - arg8.getX();
            float v3 = arg9.getY() - arg8.getY();
            if(Math.abs(arg10) <= v6 && Math.abs(arg11) <= v6) {
                return v0;
            }

            this.d(v2 / v5, v3 / v5);
            this.invalidate();
            v0 = true;
        }

        return v0;
    }

    public boolean getDoubleTapEnabled() {
        return this.d;
    }

    protected GestureDetector.OnGestureListener getGestureListener() {
        return new GestureListener(this);
    }

    protected ScaleGestureDetector.OnScaleGestureListener getScaleListener() {
        return new c(this);
    }

    public boolean onTouchEvent(MotionEvent arg5) {
        int v0 = arg5.getAction();
        if((v0 & 255) == 0) {
            this.g = true;
        }

        if(arg5.getPointerCount() > 1) {
            this.g = false;
        }

        this.a.onTouchEvent(arg5);
        if(!this.a.isInProgress()) {
            this.n.onTouchEvent(arg5);
        }

        switch(v0 & 255) {
            case 0: {
                this.m = false;
                break;
            }
            case 1: {
                if(this.getScale() >= this.getMinZoom()) {
                    return true;
                }

                this.b(this.getMinZoom(), 50f);
                break;
            }
        }

        return true;
    }

    public void setDoubleTapEnabled(boolean arg1) {
        this.d = arg1;
    }

    public void setDoubleTapListener(b arg1) {
        this.f = arg1;
    }

    public void setMyOnClickListener(View.OnClickListener arg1) {
        this.h = arg1;
    }

    public void setScaleEnabled(boolean arg1) {
        this.e = arg1;
    }

    public void setScrollEnabled(boolean arg1) {
        this.r = arg1;
    }
}

